﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Project01
{
    public enum Step { Step1, Step2, Step3, Step4, Step5, Step6, Step7 }
   class Boss01:Monster
    {
       private int[] ArrPos = new int[2];
       private TimeSpan TimeStep;
       private Step step = Step.Step1;
       private float Angle;
       private TimeSpan TimeShoot;
       private bool CreateBullets;
       private byte tam;
       private MonsterMove M=MonsterMove.Left;
        public Boss01(Game game)
            : base(game)
        {
            this.ObjectTexture = game.Content.Load<Texture2D>("Images/Boss/Boss1");
            this.Sprite = new AnimationSprite(ObjectTexture, 1, 1);
            this.Speed = 5;
            this.Health = 50;
            this.game = game;
            Type = 249;
            ArrPos[0] = 0 - Sprite.RecHeight;
            ArrPos[1] = game.Window.ClientBounds.Height + Sprite.RecHeight;
        }

        private void LoadBullets( )
        {
           // if (Couter ==1)
           // {
                CreateBulletsconsecutive(new Vector2(Sprite.Position.X + 10, Sprite.Position.Y + 10), Angle);
           // }
            //else if (Couter > 75 && Couter <= 90)
            //{
            //    CreateBulletsCirCle(13, .5f, new Vector2(Sprite.Position.X + 10, Sprite.Position.Y + 10));
            //}
            //else
            //{
            //    CreateBulletsCirCle(10, 0.3f, new Vector2(Sprite.Position.X + 10, Sprite.Position.Y + 10));
            //}

        }
        public override void Update(GameTime gameTime)
        {
            Angle += (float)gameTime.ElapsedGameTime.TotalSeconds;
            
           // Exp.Sprite.Position = SetPositionExplosion(Sprite.Position,-5,0);

            switch (step)
            {
                case Step.Step1:
                    {
                        Sprite.Position += new Vector2(2.5f, 2.5f);
                        if (Sprite.Position.Y >= game.Window.ClientBounds.Height / 10)
                            step = Step.Step2;
                        break;
                    }
                case Step.Step2:
                    {
                        TimeStep += gameTime.ElapsedGameTime;
                        CreateBullets = true;
                        TimeShoot += gameTime.ElapsedGameTime;
                        Sprite.Position = Move.MoveHorizontal(Sprite.Position, Speed);
                        if (TimeStep >= TimeSpan.FromSeconds(Ran.ran.Next(8, 15)))
                            step = Step.Step3;
                        break;
                    }
                case Step.Step3:
                    {
                        TimeStep += gameTime.ElapsedGameTime;

                        Sprite.Position = dichuyen1();
                       
                        if (Sprite.Position.Y <= 0 - Sprite.RecWidth || Sprite.Position.Y >= Sprite.RecWidth + game.Window.ClientBounds.Height)
                        {
                            if (TimeStep >= TimeSpan.FromSeconds(Ran.ran.Next(20, 25)))
                            {
                                Sprite.Position = new Vector2(100, -100);
                                step = Step.Step4;
                                TimeStep = TimeSpan.Zero;
                            }
                        }
                        break;
                    }
                case Step.Step4:
                    {
                        Sprite.Position += new Vector2(0, 5f);
                        if (Sprite.Position.Y >= game.Window.ClientBounds.Height / 5)
                            step = Step.Step5;
                        break;
                    }
                case Step.Step5:
                    {
                        TimeStep += gameTime.ElapsedGameTime;

                        CreateBullets = true;
                        TimeShoot += gameTime.ElapsedGameTime;
                        Sprite.Position = Move.MoveParaPol(Sprite.Position, Speed / 2, Angle);
                        if (TimeStep >= TimeSpan.FromSeconds(Ran.ran.Next(17, 19)))
                        {
                            step = Step.Step6;
                            TimeStep = TimeSpan.Zero;
                        }


                        break;
                    }
                case Step.Step6:
                    {
                      Sprite.Position += new Vector2(0, 15f);
                        if (Sprite.Position.Y >= Sprite.RecHeight + game.Window.ClientBounds.Height)
                        {
                            Sprite.Position = new Vector2(100, -100);
                            step = Step.Step1;
                        }

                        break;
                    }

            }

            if (CreateBullets)
            {
                if (TimeShoot >= TimeSpan.FromSeconds(0.5f))
                {
                    LoadBullets();

                    TimeShoot = TimeSpan.Zero;
                }
            }
            if (Sprite.Position.X < 0 || Sprite.Position.X > game.Window.ClientBounds.Width - Sprite.RecWidth)
            {

                Speed = -Speed;

            }

                    UpdateBullets(5);
            base.Update(gameTime);
        }
        private Vector2 dichuyen1()
        {
            Vector2 Move = Vector2.Zero;
            if (tam == 0)
            {
                Move = new Vector2(0, 15);

            }
            else if (tam == 1)
            {
                Move = new Vector2(0, -15);
            }
            if (Sprite.Position.Y >= game.Window.ClientBounds.Height + Sprite.RecHeight ||
               Sprite.Position.Y <= 0 - Sprite.RecHeight)
            {
                tam = (byte)Ran.ran.Next(0, 2);
                Sprite.Position = new Vector2(Ran.ran.Next(0 + Sprite.RecWidth, game.Window.ClientBounds.Width - Sprite.RecWidth), ArrPos[tam]);

            }
            Sprite.Position += Move;
            return Sprite.Position;
        }
        public override void Draw(SpriteBatch SpriteBatch)
        {
            for (int i = 0; i < Mbullets.Count; i++)
                Mbullets[i].Draw(SpriteBatch);
            base.Draw(SpriteBatch);
        }
        private void CreateBulletsconsecutive(Vector2 Position, float angle)
        {
            for (int i = 1; i <= 2; i++)
            {
                Bullets B = new Bullets(game, "Images/Bullets/Monster/B_M_Type03",1,1);
                B.Active = true;
                B.Sprite.Position = new Vector2(Position.X+(50*i), Sprite.Position.Y);
                angle = (float)Math.PI / Ran.NextFloat(Ran.ran, 1.2f, 2.5f);
                B.Sprite.Rotation= angle;
                Mbullets.Add(B);
            }
        }
        private void CreateBulletsCirCle(int number, float angle, Vector2 Position)
        {
            for (int i = 1; i <= number; i++)
            {
                Bullets B = new Bullets(game, "Images/Bullets/Monster/B_M_Type03", 1, 1);
                B.Sprite.Rotation = angle*i;
                B.Active = true;
                B.Sprite.Position = Position;
                Mbullets.Add(B);
            }
        }
        
        private void UpdateBullets(float BulletsSpeed)
        {
            for (int i = 0; i < Mbullets.Count; i++)
            {
                Mbullets[i].Sprite.Update();
                Mbullets[i].Sprite.Position += new Vector2(Move.toAngle(Mbullets[i].Sprite.Rotation).X * BulletsSpeed, Move.toAngle(Mbullets[i].Sprite.Rotation).Y * BulletsSpeed);
                if (Mbullets[i].Sprite.Position.X <= 0 - Mbullets[i].Sprite.RecWidth || Mbullets[i].Sprite.Position.X >= game.Window.ClientBounds.Width + Mbullets[i].Sprite.RecWidth||
                   Mbullets[i].Sprite.Position.Y <= 0 - Mbullets[i].Sprite.RecHeight|| Mbullets[i].Sprite.Position.Y >= game.Window.ClientBounds.Height + Mbullets[i].Sprite.RecHeight)
                {
                    Mbullets[i] = null;
                    Mbullets.RemoveAt(i);
                    i--;
                }
            }
        }
    }
}
